package com.qbw.app.manager;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.net.Uri;
import android.os.Bundle;

import com.qbw.app.XApplication;
import com.qbw.l.L;
import com.qbw.log.XLog;

import java.util.List;

import pub.devrel.easypermissions.EasyPermissions;

/**
 * @author qinbaowei
 * @date 2018/3/19
 * @email qbaowei@qq.com
 */

public class LocationManager {
    private static LocationManager sInst;

    private android.location.LocationManager mLocationManager;

    private LocationManager() {
        mLocationManager = (android.location.LocationManager) XApplication.getInstance()
                                                                          .getSystemService(Context.LOCATION_SERVICE);
    }

    public static LocationManager getInstance() {
        if (sInst == null) {
            synchronized (LocationManager.class) {
                if (sInst == null) {
                    sInst = new LocationManager();
                }
            }
        }
        return sInst;
    }

    public void requestLocationChanged() {
        mLocationManager.requestLocationUpdates(android.location.LocationManager.GPS_PROVIDER,
                                                2000,
                                                0,
                                                new LocationListener() {
                                                    @Override
                                                    public void onLocationChanged(Location location) {
                                                        XLog.d("lat:%f,lon:%f", location.getLatitude(), location.getLongitude());
                                                    }

                                                    @Override
                                                    public void onStatusChanged(String provider,
                                                                                int status,
                                                                                Bundle extras) {

                                                    }

                                                    @Override
                                                    public void onProviderEnabled(String provider) {

                                                    }

                                                    @Override
                                                    public void onProviderDisabled(String provider) {

                                                    }
                                                });
    }

    public Location getLastLocation() {
        try {
            String locationProvider;
            //获取所有可用的位置提供器
            List<String> providers = mLocationManager.getProviders(true);

            for (String provider : providers) {
                //获取Location
                Location location = mLocationManager.getLastKnownLocation(provider);
                if (location != null && location.getLatitude() != 0 && location.getLongitude() != 0) {
                    return location;
                } else {
                    mLocationManager.requestSingleUpdate(provider,
                                                         PendingIntent.getBroadcast(XApplication.getInstance(),
                                                                                    0,
                                                                                    new Intent(
                                                                                            "broadcast_last_location_changed"),
                                                                                    0));

                }
                L.GL.w("provider[%s] location is null", provider);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void showCurrentLocation() {
        Location location = getLastLocation();
        if (location != null) {
            XLog.d("lat:%f,lon:%f", location.getLatitude(), location.getLongitude());
        }
    }

    /**
     * 判断GPS是否开启，GPS或者AGPS开启一个就认为是开启的
     *
     * @param context
     * @return true 表示开启
     */
    public final boolean isGpsOpened(final Context context) {
        return mLocationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER);
    }

    /**
     * 强制帮用户打开GPS
     *
     * @param context
     *//*
    public final void openGPS(Context context) {
        try {
            Intent GPSIntent = new Intent();
            GPSIntent.setClassName("com.android.settings",
                                   "com.android.settings.widget.SettingsAppWidgetProvider");
            GPSIntent.addCategory("android.intent.category.ALTERNATIVE");
            GPSIntent.setData(Uri.parse("custom:3"));
            PendingIntent.getBroadcast(context, 0, GPSIntent, 0).send();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

}
